package com.bitspilani.infonet.serviceinterface;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;


public class ServiceInvoker {
	

	public static String invokeServie(String serviceUrl, String jsonPayload) throws Exception{


		HttpPost httppost = new HttpPost(serviceUrl);
		httppost.setHeader("Content-Type","application/json");

		/*		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
		HttpProtocolParams.setUseExpectContinue(params, false);
		httppost.setParams(params);


			List<NameValuePair> formparams = new ArrayList<NameValuePair>();
			formparams.add(new BasicNameValuePair("userid", param1));
			formparams.add(new BasicNameValuePair("password", param2));
			UrlEncodedFormEntity postEntity = null;
			try {
				postEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
				httppost.setEntity(postEntity);
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
		trustStore.load(null, null);

		SSLSocketFactory sf = new SslSocketFactory(trustStore);
		sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

		SchemeRegistry registry = new SchemeRegistry();
		registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
		registry.register(new Scheme("https", sf, 443));

		ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
*/
		HttpEntity bodyEntity = new ByteArrayEntity(jsonPayload.getBytes("UTF-8"));
		httppost.setEntity(bodyEntity);
		
		HttpClient client = new DefaultHttpClient();//new DefaultHttpClient(ccm,params);

		HttpResponse httpResponse;
		httpResponse = client.execute(httppost);

		int responseCode = httpResponse.getStatusLine().getStatusCode();
		String message = httpResponse.getStatusLine().getReasonPhrase();
		System.out.println("Service Response: "+responseCode+", message: "+message);
		HttpEntity entity = httpResponse.getEntity();
		
		String serviceResponse = "", temp;
		if (entity != null) {
			InputStream instream = entity.getContent();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(instream));
			while((temp=br.readLine())!=null){
				serviceResponse += temp;
			}
			instream.close();
			br.close();
			
			return serviceResponse;
		}
		
		return null;

	}

}
